﻿using Kaliko.ImageLibrary;
using Kaliko.ImageLibrary.Filters;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace PsAutomation
{
    public static class Utility
    {
        public static Image CropFromCenter(this Image bmp, int size)
        {
            Rectangle cropRect = new Rectangle((bmp.Width / 2) - (size / 2), (bmp.Height / 2) - (size / 2), size, size);

            Bitmap target = new Bitmap(size, size);

            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(bmp, new Rectangle(0, 0, target.Width, target.Height),
                                 cropRect,
                                 GraphicsUnit.Pixel);
            }

            return target;
        }

        public static Image Resize(this Image img, int size)
        {
            //a holder for the result
            Bitmap result = new Bitmap(size, size);
            //set the resolutions the same to avoid cropping due to resolution differences
            result.SetResolution(img.HorizontalResolution, img.VerticalResolution);

            //use a graphics object to draw the resized image into the bitmap
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //draw the image into the target bitmap
                graphics.DrawImage(img, 0, 0, result.Width, result.Height);
            }

            return result as Image;
        }

        public static byte[] ToJPEG(this Image img)
        {
            byte[] bytes;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, ImageFormat.Jpeg);
                    bytes = ms.ToArray();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return bytes;
        }

        public static Image Unsharpen(this Image img, float strength = 0.4F)
        {
            //KalikoImage sharpimg = new KalikoImage(img).GetThumbnailImage(427, 284, ThumbnailMethod.Fit);
            //sharpimg.ApplyFilter(new UnsharpMaskFilter(1.2, 0.3));
            //sharpimg.SaveJpg(pathT, 95);

            return new KalikoImage(img).Unsharpen(strength);
        }

        public static Image Unsharpen(this KalikoImage img, float strength = 0.4F)
        {
            img.ApplyFilter(new UnsharpMaskFilter(1.2F, strength, 20));   //threshold: http://www.damiensymonds.com.au/tut_usm.html

            MemoryStream ms = new MemoryStream();
            img.SaveJpg(ms, 90);
            return Image.FromStream(ms);
        }

    }
}
